%%@ author Grupp 3
%%@ version 1.5
%%@ copyright Grupp 3
%%@ doc The module for starting the main server. 

-module(mainServer).

-export([start/0, restart/0]).

%%@doc spawns a new server and register it as "pronection_server" and sends projList to the projectServer:start_servers() function.
%%@spec start() -> atom()

start() ->
	dets:open_file(projects,[]),
	dets:close(projects),
    ServerPid = spawn(fun() -> server_loop(projects) end),
    register(pronection_server, ServerPid),
	{ok, ProjList} = proman:listProjects(projects),
	projectServer:start_servers(ProjList).

%%@doc spawns a new server and register it as "pronection_server" and starts server_loop with projects.
%%@spec restart() -> atom()

restart() ->
	dets:open_file(projects,[]),
	dets:close(projects),
    ServerPid = spawn(fun() -> server_loop(projects) end),
    register(pronection_server, ServerPid).	

%%@doc Server loop keeps the server alive and repons to call from client
%%@spec server_loop(ProjectList) -> any()
%% where
%%	ProjectList = atom()

server_loop(ProjectList) -> 
    receive

	{newUser, User, Password, ClientPid} ->
		io:format("Received a request from client ~p, to register user ~s with password ~s. ~n",[ClientPid, User, Password]),
		case userman:addUser(User, Password) of
			ok ->
				ClientPid ! {ok, self()};
			{error, Mes} ->
				io:format("~s ~n", [Mes]),		
				ClientPid ! {false, self()}
		end,
		server_loop(ProjectList);
	

	{login, User, Password, ClientPid} ->
		io:format("Received a request from client ~p, to login as user ~w with password ~w. ~n",[ClientPid, User, Password]),
		case userman:login(User, Password) of
			ok ->
				ClientPid ! {ok, self()};
			{error, Mes} ->
				io:format("~s ~n", [Mes]),
				ClientPid ! {false, self()}
		end,
		server_loop(ProjectList);	

		
	{addProject, Name, ClientPid} ->
		io:format("Received a request from client ~p, to add project ~s. ~n",[ClientPid, Name]),
		case proman:newProject(ProjectList, Name) of
			{ok, ProjectId} -> 
				ClientPid ! {ok, self()},
				projectServer:start_servers([{Name ,ProjectId}]);
			{error, Mes} -> 	
				io:format("~n~nPROJECT NAME ALREADY EXISTS!!!!~n~n"),
				ClientPid ! {error, Mes, self()};
			_ -> 
				io:format("error~n",[])
		end,
		server_loop(ProjectList);

	{listProjects, ClientPid} ->
		io:format("Received a request from client ~p, to list all projects.~n", [ClientPid]),		
		{ok, ProjList} = proman:listProjects(ProjectList),
		ClientPid ! {ok, ProjList, self()},
		server_loop(ProjectList);

	{crash, ClientPid} -> 
			ClientPid ! {ok, self()},
			io:format("MainServer crashed ~n",[]),
			erlang:raise(exit, "Boom!", []);

	{check , ClientPid} ->
		ClientPid ! {ok, self()},
		server_loop(ProjectList);

	{deleteProject, ProjectName, ClientPid} ->
		io:format("Received a request from client ~p, to delete project ~s.~n",[ClientPid, ProjectName]),
		case proman:deleteProject(ProjectList, ProjectName) of
			{ok, ProjectId} ->
				{list_to_atom(hd(io_lib:format("~w",[ProjectId]))), pronection_server@hamberg.it.uu.se} ! {kill , self()}, 
				ClientPid ! {ok, self()};
			{error, Mes} ->
				ClientPid ! {error, Mes, self()}
		end,
		server_loop(ProjectList)
    after 20000 ->
    	io:format("MainServer is still running~n",[]),
	server_loop(ProjectList)
    end.		

